package cn.codeforfun.modules.user.list.table;

import cn.codeforfun.modules.user.User;
import org.springframework.stereotype.Component;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

@Component
public class ListTableModel extends AbstractTableModel {
    protected final List<User> dataList = new ArrayList<>();

    public String[] getColumnLabels() {
        return new String[]{
                "用户名",
                "密码",
                "邮箱"
        };
    }

    @Override
    public String getColumnName(int column) {
        return getColumnLabels()[column];
    }

    @Override
    public int getRowCount() {
        return dataList.size();
    }

    @Override
    public int getColumnCount() {
        return getColumnLabels().length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        User user = dataList.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return user.getUsername();
            case 1:
                return user.getPassword();
            case 2:
                return user.getEmail();
            default:
                return "";
        }
    }

    public void clear() {
        dataList.clear();
    }

    public void addDataList(List<User> users) {
        this.dataList.addAll(users);
        fireTableDataChanged();
    }
}
